package show.me.the.money.service.core;


import java.util.HashMap;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import show.me.the.money.accredit.User;
import show.me.the.money.config.app.AppConfigController;
import show.me.the.money.config.app.Application;
import show.me.the.money.util.context.Context;


public class AppConfigLocator  implements Service{
	private static Log logger = LogFactory.getLog(AppConfigLocator.class);
	public void execute(HashMap<String,Object> req,HashMap<String,Object> res,Context ctx){
		User user = (User)ctx.get("user.instance");
		String id = (String)req.get("id");
		int code = 200;
		String msg = "";
		if(StringUtils.isEmpty(id)){
			logger.error("module id missing");
			code = 401;
			msg = "ModuleIdMissing";
		}
		else{
			Application  app = AppConfigController.instance().getApplication(id);
			if(app == null){
				code = 404;
				msg = "AppNotFound";	
			}
			else{
				res.put("body",Logon.filter(app,user,ctx));
			}
		}
		res.put(Service.RES_CODE,code);
		res.put(Service.RES_MESSAGE,msg);	
	}
}
